﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Charisma
{
    public abstract class Repository : IRepository
    {
        internal Repository()
        {
        }

        #region IRepository Members

        public abstract Type ElementType { get; }

        #endregion
    }

    public class Repository<T> : Repository, IRepository<T> where T : class
    {
        private readonly DbContext context;
// ReSharper disable UnassignedField.Global
        internal IRepository<T> implementation;
// ReSharper restore UnassignedField.Global

        public Repository(DbContext context)
        {
            this.context = context;
        }

        public DbContext DbContext
        {
            get { return context; }
        }

        #region IRepository<> implementation

        public override Type ElementType
        {
            get { return typeof (T); }
        }

        public IQueryable<T> All
        {
            get { return implementation.All; }
        }

        public T Find(object key)
        {
            return implementation.Find(key);
        }

        public void Add(T item)
        {
            implementation.Add(item);
        }

        public void Add(IEnumerable<T> items)
        {
            implementation.Add(items);
        }

        public void Remove(T item)
        {
            implementation.Remove(item);
        }

        public void Update(T item)
        {
            implementation.Update(item);
        }

        public void Update(T item, params Expression<Func<T, object>>[] fields)
        {
            implementation.Update(item,fields);
        }

        #endregion
    }
}